package com.lc.servlet;

import com.lc.bean.Users;
import com.lc.service.UsersService;
import com.lc.service.impl.UsersServiceImpl;
import com.lc.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @description:用于进行用户（非学生）查询等功能的操作
 * @author: ManolinCoder
 * @time: 2021/8/24
 */
@WebServlet("/power/user/users")
public class UsersServlet extends HttpServlet {
    private UsersService usersService = new UsersServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("select".equals(method)){
            select(req,resp);
        }else if ("add".equals(method)){
            add(req,resp);
        }
    }

    //查询数据(分页)
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数(分页参数+模糊查询参数)
        String index = req.getParameter("index");
        int pageIndex = (index==null||index.length()==0)?1:Integer.parseInt(index);
        //2.调取service方法(1.查询数据列表的方法、2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Users> usersList = usersService.getUsersList(pageIndex, pageUtil.getPageSize());
        int total = usersService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(usersList);

        //3.存值并跳转页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }

    //新增用户
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面数据
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String roleId = req.getParameter("roleId");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String cardId = req.getParameter("cardId");
        String desc = req.getParameter("desc");
        //调取service中的增加方法
        Users users = new Users();
        users.setLoginName(loginName);
        users.setPassWord(password);
        users.setRealName(realName);
        users.setSex(Integer.parseInt(sex));
        users.setRoleId(Integer.parseInt(roleId));
        users.setEmail(email);
        users.setPhone(phone);
        users.setAddress(address);
        users.setCardId(cardId);
        users.setDesc(desc);
        int i = usersService.insertUser(users);
        //最后是分情况的页面跳转，这里不能直接跳转页面否则页面中会没有数据，应当跳转相应的servlet
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('新增成功');location.href='/power/user/users?method=select';</script>");
        }else {
            writer.println("<script>alert('新增失败');location.href='/power/user/getRoleList';</script>");
        }
    }
}
